package ecmnext.server.net;

/***************************************************************/
/*                     SOFTWARE SECTION                        */
/***************************************************************/
/*
 * <p>Name: ECMNext</p>
 * <p>Description: Simulation of the platform of trading Software</p>
 * <p>Licence: General Public License</p>
 * <p>Organisation: EC Marseille</p>
 * <p>Persons involved in the project : groupe Plateforme de Trading</p>
 * <p>Current version: 1.0</p>
 */

/***************************************************************/
/*                       IMPORT SECTION                        */
/***************************************************************/

import java.io.*;
import java.net.*;

import ecmnext.server.*;
import ecmnext.utils.*;

/***************************************************************/
/*           ClientConnectionPoint CLASS SECTION               */
/***************************************************************/
/**
 * <p>Title : ClientConnectionPoint</p>
 * <p>Description : </p>
 * @author Xuewen YANG ET Antoine COUDERC
 * @version 1.0
 */

public class ClientConnectionPoint extends Thread {

  private ServerSocket serverSocket;
  private String AddressIP ;

  public ClientConnectionPoint() {
  super("ClientConnectionPoint");
    try {
      this.serverSocket = new ServerSocket(Integer.parseInt(Utils.appsProperties.getProperty("ServerWaitingPort")));
    }
    catch (NumberFormatException ex) {
      Utils.fatalError("Property ServerWaitingPort is not an integer. Could not initialise SocketServer. " + ex.toString(), 1, ex);
    }
    catch (IOException ex) {
      Utils.fatalError("An Input/output exception has occured while trying to initiate the serverSocket" + ex.toString(), 1, ex);
    }
  }

  /**
   *
   * @return String
   */
  public String getIpAddressAndJavaVersion ()
  {
    try {
      AddressIP=serverSocket.getInetAddress().getLocalHost().getHostAddress();
    }
    catch (UnknownHostException ex) {
      Utils.logger.warn("Unabled to get host IP address. [IGNORED]");
    }
    return "\""+AddressIP+"\" / \""+(System.getProperty("java.version"))+"\"";
  }


  /**
   *
   */
  public void attenteConnexion() {

   try {
     AddressIP=serverSocket.getInetAddress().getLocalHost().getHostAddress();
     Utils.logger.info("Server hostname : " + serverSocket.getInetAddress().getLocalHost().getHostName() + ", Server IP address: " + AddressIP);
   }
   catch (UnknownHostException ex) {
     Utils.logger.warn("Unabled to get host IP address. [IGNORED]");
   }

   while (Server.getServerState() == Server.SERVER_STATE_ONLINE) {

     try {
       new PreConnectionClient(serverSocket.accept()).initiatePlayer();
       // serveur.accept() wait until a client try to connect and return
       // a socket with which we will be able to communicate with the client.
       // Then we create a thread that will be dedicated to the communication
       // with this client.
     }
     catch (Exception ex1) {
       System.out.println(ex1.toString());
     }

     // Waiting next client.
   }
 }

 /**
  *
  */
 public void run() {
 this.attenteConnexion();
 }
}
